package com.apache.bid.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.bid.entity.BidApplyHistory;
import com.apache.bid.manager.BidApplyHistoryManager;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;

public class BidApplyHistoryManagerImpl implements BidApplyHistoryManager {

	protected IDao bidApplyHistoryDao;
	protected final String entityName = "com.apache.bid.entity.BidApplyHistory";

	public void setBidApplyHistoryDao(IDao bidApplyHistoryDao) {
		this.bidApplyHistoryDao = bidApplyHistoryDao;
	}

	public String saveInfo(ParamsVo<BidApplyHistory> vo) throws BusinessException {
		BidApplyHistory bidApplyHistory = vo.getObj();
		if (Validator.isEmpty(bidApplyHistory)) {
			List<BidApplyHistory> list = vo.getObjs();
			MethodParam param = new MethodParam("Batch", "", "", entityName);
			param.setVaule(list);
			if (bidApplyHistoryDao.insertBath(param)) {
				return "true";
			}
		} else {
			String infoId = Validator.generate();
			//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
			String cacheKey = "";
			//String cacheKey="bidApplyHistory_"+infoId;
			bidApplyHistory.setApplyId(infoId);
			MethodParam param = new MethodParam("BidApplyHistory", cacheKey, "", entityName);
			param.setVaule(bidApplyHistory);
			if (bidApplyHistoryDao.insert(param)) {
				return infoId;
			}
		}
		return "";
	}

	public boolean editInfo(ParamsVo<BidApplyHistory> vo) throws BusinessException {
		BidApplyHistory bidApplyHistory = vo.getObj();
		if (Validator.isNotNull(bidApplyHistory.getApplyId())) {
			String cacheKey = "";
			//String cacheKey="bidApplyHistory_"+bidApplyHistory.getApplyId();
			MethodParam param = new MethodParam("BidApplyHistory", cacheKey, "", entityName);
			param.setVaule(bidApplyHistory);
			return bidApplyHistoryDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<BidApplyHistory> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="bidApplyHistory_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		BidApplyHistory info = (BidApplyHistory) bidApplyHistoryDao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("BidApplyHistory");
			return bidApplyHistoryDao.edit(param);
		} else {
			param.setParams("applyId", infoId);
			param.setDelete(true);
			return bidApplyHistoryDao.delete(param);
		}
	}

	public Object getInfoById(ParamsVo<BidApplyHistory> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="bidApplyHistory_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return bidApplyHistoryDao.selectById(param);
	}

	public Object execute(ParamsVo<BidApplyHistory> vo) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page getPageInfo(ParamsVo<BidApplyHistory> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		String orderBy = Validator
				.getDefaultStr(String.valueOf(vo.getParams("orderBy")), " order by create_time desc ");
		param.setParams("orderBy", orderBy);
		Page page = bidApplyHistoryDao.pageSelect(param);
		return page;
	}

	public List<BidApplyHistory> getList(ParamsVo<BidApplyHistory> vo) {
		MethodParam param = setMethodParams(vo, 2);
		String orderBy = Validator
				.getDefaultStr(String.valueOf(vo.getParams("orderBy")), " order by create_time desc ");
		param.setParams("orderBy", orderBy);
		return bidApplyHistoryDao.select(param);
	}

	public long countInfo(ParamsVo<BidApplyHistory> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return bidApplyHistoryDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<BidApplyHistory> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(bidApplyHistoryDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		param.setResultCache(false);
		BidApplyHistory bidApplyHistory = vo.getObj();
		if (Validator.isNotNull(bidApplyHistory.getSessionId())) {
			sb.append(" and sessionId = :sessionId");
			param.setParams("sessionId", bidApplyHistory.getSessionId());
		}
		if (Validator.isNotNull(bidApplyHistory.getUserType())) {
			sb.append(" and userType = :userType");
			param.setParams("userType", bidApplyHistory.getUserType());
		}
		if (Validator.isNotNull(bidApplyHistory.getUserId())) {
			sb.append(" and userId = :userId");
			param.setParams("userId", bidApplyHistory.getUserId());
		}
		if (Validator.isNotNull(bidApplyHistory.getItemId())) {
			sb.append(" and itemId = :itemId");
			param.setParams("itemId", bidApplyHistory.getItemId());
		}
		if (Validator.isNotNull(bidApplyHistory.getRole())) {
			sb.append(" and role = :role");
			param.setParams("role", bidApplyHistory.getRole());
		}
		if (Validator.isNotNull(bidApplyHistory.getBidUserNo())) {
			sb.append(" and bidUserNo = :bidUserNo");
			param.setParams("bidUserNo", bidApplyHistory.getBidUserNo());
		}
		if (Validator.isNotNull(bidApplyHistory.getCreateTime())) {
			sb.append(" and createTime = :createTime");
			param.setParams("createTime", bidApplyHistory.getCreateTime());
		}
		if (Validator.isNotNull(bidApplyHistory.getModifyTime())) {
			sb.append(" and modifyTime = :modifyTime");
			param.setParams("modifyTime", bidApplyHistory.getModifyTime());
		}
		if (Validator.isNotNull(bidApplyHistory.getIfRight())) {
			sb.append(" and ifRight = :ifRight");
			param.setParams("ifRight", bidApplyHistory.getIfRight());
		}
		if (Validator.isNotNull(bidApplyHistory.getUserName())) {
			sb.append(" and userName = :userName");
			param.setParams("userName", " like '%" + bidApplyHistory.getUserName() + "%' ");
		}
		if (Validator.isNotNull(bidApplyHistory.getRealName())) {
			sb.append(" and realName = :realName");
			param.setParams("realName", bidApplyHistory.getRealName());
		}
		if (Validator.isNotNull(bidApplyHistory.getAllowBuyNum())) {
			sb.append(" and allowBuyNum = :allowBuyNum");
			param.setParams("allowBuyNum", bidApplyHistory.getAllowBuyNum());
		}
		if (Validator.isNotNull(bidApplyHistory.getSczlTime())) {
			sb.append(" and sczlTime = :sczlTime");
			param.setParams("sczlTime", bidApplyHistory.getSczlTime());
		}
		if (Validator.isNotNull(bidApplyHistory.getApplyWay())) {
			sb.append(" and applyWay = :applyWay");
			param.setParams("applyWay", bidApplyHistory.getApplyWay());
		}
		if (Validator.isNotNull(bidApplyHistory.getReadRule())) {
			sb.append(" and readRule = :readRule");
			param.setParams("readRule", bidApplyHistory.getReadRule());
		}
		if (Validator.isNotNull(bidApplyHistory.getCustomerNo())) {
			sb.append(" and customerNo = :customerNo");
			param.setParams("customerNo", bidApplyHistory.getCustomerNo());
		}
		if (Validator.isNotNull(bidApplyHistory.getCustomerName())) {
			sb.append(" and customerName = :customerName");
			param.setParams("customerName", bidApplyHistory.getCustomerName());
		}
		if (Validator.isNotNull(bidApplyHistory.getCustomerStatus())) {
			sb.append(" and customerStatus = :customerStatus");
			param.setParams("customerStatus", bidApplyHistory.getCustomerStatus());
		}
		if (Validator.isNotNull(bidApplyHistory.getProvince())) {
			sb.append(" and province = :province");
			param.setParams("province", bidApplyHistory.getProvince());
		}
		if (Validator.isNotNull(bidApplyHistory.getCity())) {
			sb.append(" and city = :city");
			param.setParams("city", bidApplyHistory.getCity());
		}
		if (Validator.isNotNull(bidApplyHistory.getDist())) {
			sb.append(" and dist = :dist");
			param.setParams("dist", bidApplyHistory.getDist());
		}
		if (Validator.isNotNull(bidApplyHistory.getPhone())) {
			sb.append(" and phone = :phone");
			param.setParams("phone", bidApplyHistory.getPhone());
		}
		if (Validator.isNotNull(bidApplyHistory.getMobile())) {
			sb.append(" and mobile = :mobile");
			param.setParams("mobile", bidApplyHistory.getMobile());
		}
		if (Validator.isNotNull(bidApplyHistory.getCertType())) {
			sb.append(" and certType = :certType");
			param.setParams("certType", bidApplyHistory.getCertType());
		}
		if (Validator.isNotNull(bidApplyHistory.getCertNo())) {
			sb.append(" and certNo = :certNo");
			param.setParams("certNo", bidApplyHistory.getCertNo());
		}
		if (Validator.isNotNull(bidApplyHistory.getEmail())) {
			sb.append(" and email = :email");
			param.setParams("email", bidApplyHistory.getEmail());
		}
		if (Validator.isNotNull(bidApplyHistory.getFax())) {
			sb.append(" and fax = :fax");
			param.setParams("fax", bidApplyHistory.getFax());
		}
		if (Validator.isNotNull(bidApplyHistory.getCompany())) {
			sb.append(" and company = :company");
			param.setParams("company", bidApplyHistory.getCompany());
		}
		if (Validator.isNotNull(bidApplyHistory.getBailPayMode())) {
			sb.append(" and bailPayMode = :bailPayMode");
			param.setParams("bailPayMode", bidApplyHistory.getBailPayMode());
		}
		if (Validator.isNotNull(bidApplyHistory.getBank())) {
			sb.append(" and bank = :bank");
			param.setParams("bank", bidApplyHistory.getBank());
		}
		if (Validator.isNotNull(bidApplyHistory.getBankAccount())) {
			sb.append(" and bankAccount = :bankAccount");
			param.setParams("bankAccount", bidApplyHistory.getBankAccount());
		}
		if (Validator.isNotNull(bidApplyHistory.getShouldBail())) {
			sb.append(" and shouldBail = :shouldBail");
			param.setParams("shouldBail", bidApplyHistory.getShouldBail());
		}
		if (Validator.isNotNull(bidApplyHistory.getFactBail())) {
			sb.append(" and factBail = :factBail");
			param.setParams("factBail", bidApplyHistory.getFactBail());
		}
		if (Validator.isNotNull(bidApplyHistory.getFactBailTime())) {
			sb.append(" and factBailTime = :factBailTime");
			param.setParams("factBailTime", bidApplyHistory.getFactBailTime());
		}
		if (Validator.isNotNull(bidApplyHistory.getFactBailAccount())) {
			sb.append(" and factBailAccount = :factBailAccount");
			param.setParams("factBailAccount", bidApplyHistory.getFactBailAccount());
		}
		if (Validator.isNotNull(bidApplyHistory.getBailFinshFlag())) {
			sb.append(" and bailFinshFlag = :bailFinshFlag");
			param.setParams("bailFinshFlag", bidApplyHistory.getBailFinshFlag());
		}
		if (Validator.isNotNull(bidApplyHistory.getStatus())) {
			sb.append(" and status = :status");
			param.setParams("status", bidApplyHistory.getStatus());
		}
		if (Validator.isNotNull(bidApplyHistory.getOrgEname())) {
			sb.append(" and orgEname = :orgEname");
			param.setParams("orgEname", bidApplyHistory.getOrgEname());
		}
		if (Validator.isNotNull(bidApplyHistory.getPayFlowNo())) {
			sb.append(" and payFlowNo = :payFlowNo");
			param.setParams("payFlowNo", bidApplyHistory.getPayFlowNo());
		}
		if (Validator.isNotNull(bidApplyHistory.getSpare1())) {
			sb.append(" and spare1 = :spare1");
			param.setParams("spare1", bidApplyHistory.getSpare1());
		}
		if (Validator.isNotNull(bidApplyHistory.getSpare2())) {
			sb.append(" and spare2 = :spare2");
			param.setParams("spare2", bidApplyHistory.getSpare2());
		}
		if (Validator.isNotNull(bidApplyHistory.getSpare3())) {
			sb.append(" and spare3 = :spare3");
			param.setParams("spare3", bidApplyHistory.getSpare3());
		}
		if (Validator.isNotNull(bidApplyHistory.getSpare4())) {
			sb.append(" and spare4 = :spare4");
			param.setParams("spare4", bidApplyHistory.getSpare4());
		}
		if (Validator.isNotNull(bidApplyHistory.getSpare5())) {
			sb.append(" and spare5 = :spare5");
			param.setParams("spare5", bidApplyHistory.getSpare5());
		}
		if (Validator.isNotNull(bidApplyHistory.getObjName())) {
			sb.append(" and objName  like '%" + bidApplyHistory.getObjName() + "%' ");
			param.setParams("objName", " like '%" + bidApplyHistory.getObjName() + "%' ");
		}
		if (Validator.isNotNull(bidApplyHistory.getApplyName())) {
			sb.append(" and applyName = :applyName");
			param.setParams("applyName", bidApplyHistory.getApplyName());
		}

		param.setSqlStr(sb.toString());
		return param;
	}
}
